package chap04.Infix;

/**
 * Created by root on 2017/7/5.
 */
class StackO{
    private int maxSize;
    private Object[] stackArray;
    private int top;

    public StackO(int maxSize) {
        this.maxSize = maxSize;
        stackArray = new Object[maxSize];
        top = -1;
    }

    public void push(Object j) {
        stackArray[++top] = j;
    }

    public Object pop() {
        return stackArray[top--];
    }

    public Object peek() {
        return stackArray[top];
    }

    public boolean isEmpty() {
        return (top == -1);
    }

    public boolean isFull() {
        return (top == maxSize-1);
    }

    public int size() {
        return (top + 1);
    }
}
public class Infix {

    private StackO numStack = null;

    private StackO charStack = null;

    private StackO postfix = null;

    public Infix(){
        numStack = new StackO(10);
        charStack = new StackO(10);
        postfix = new StackO(20);
    }

    public static void main(String[] agrs) {
        String expression = "3+2*(6-8/(1+1))";
        Infix infix = new Infix();
        System.out.println(infix.doCount(expression));
    }

    public int doCount(String expression) {

        char[] cs = expression.toCharArray();
        for (int i=0; i<cs.length; i++) {
            char z = cs[i];
            if (isNumber((int)z)) {
                numStack.push(z);
            } else {
                //遇到符号的时候需要计算，比较优先级

                if (charStack.isEmpty()) {
                    charStack.push(z);
                } else {
                    char c = (char)charStack.peek();
                    if (isFirst(c,z)) {
                        //计算
                        calculate();
                        if (z == ')') {
                            char ce = (char)charStack.peek();
                            while (ce!='(') {
                                calculate();
                                ce = (char)charStack.peek();
                            }
                            charStack.pop();
                        } else {
                            charStack.push(z);
                        }
                    } else {
                        charStack.push(z);
                    }
                }

            }
        }
        // 计算
        int a = 0;
        while (!charStack.isEmpty()){
            calculate();
        }
        return (int)numStack.pop();
    }

    public boolean isNumber(Object o) {
        if(o == null) return false;
        if(o instanceof Number) {
            int a = (int)o;
            if (a>39 && a<48) {
                return false;
            }
            return true;
        }
        if(o instanceof String) {
            String str = (String)o;
            if(str.length() == 0) return false;
            if(str.trim().length() == 0) return false;

            try {
                Double.parseDouble(str);
                return true;
            }catch(NumberFormatException e) {
                return false;
            }
        }
        return false;
    }

    // 比较符号的优先级
    public boolean isFirst(char a, char b) {
        if ((a == '+'|| a=='-') && (b=='*'||b=='/')){
            return false;
        } else if (a == '(' || b == '('){
            return false;
        } else if (b == ')'){
            return true;
        }
        return true;
    }

    public void calculate() {
        int b = Integer.parseInt(numStack.pop().toString());
        int a = Integer.parseInt(numStack.pop().toString());
        char c = (char)charStack.pop();
        int d = 0;
        switch (c) {
            case '+' :
                 d = a+b;
                 break;
            case '-' :
                d = a-b;
                break;
            case '*' :
                d = a*b;
                break;
            case '/' :
                d = a/b;
                break;
        }
        numStack.push(d);
    }


}
